package com.quectel.core.module.notice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.notice.dao.NoticeVillageDao;
import com.quectel.core.module.notice.dto.NoticeVillageDto;
import com.quectel.core.module.notice.entity.NoticeVillageEntity;
import com.quectel.core.module.notice.service.NoticeVillageService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-27 10:05:07
 */
@DubboService
public class NoticeVillageServiceImpl implements NoticeVillageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeVillageServiceImpl.class);

    private static final String NOTICE_VILLAGE_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "NOTICE_VILLAGE_CACHE_BY_ID:";

    @Autowired
    private NoticeVillageDao noticeVillageDao;
    @Autowired
    private VillageService villageService;

    /**
     * 通过 noticeId 查询绑定小区list
     *
     * @param noticeId
     * @return
     */
    @Override
    public List<VillageDto> selectByNoticeId(Long noticeId) {
        List<NoticeVillageEntity> entityList =
                noticeVillageDao.selectList(new LambdaQueryWrapper<NoticeVillageEntity>()
                        .eq(NoticeVillageEntity::getNoticeId, noticeId));
        return entityList.stream().map(v -> villageService.selectById(v.getVillageId()))
                .filter(v -> v != null).collect(Collectors.toList());
    }

    @Override
    public NoticeVillageDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                NOTICE_VILLAGE_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public NoticeVillageDto selectById(Long id) {
        NoticeVillageEntity entity = noticeVillageDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, NoticeVillageDto.class));
    }

    @Override
    public List<NoticeVillageDto> queryList(Map<String, Object> params) {
        List<NoticeVillageEntity> list = noticeVillageDao.queryList(params);
        List<NoticeVillageDto> result = CopyUtils.copyList(list, NoticeVillageDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private NoticeVillageDto paddingField(NoticeVillageDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return noticeVillageDao.queryTotal(params);
    }

    @Override
    public Long save(NoticeVillageDto dto) {
        NoticeVillageEntity entity = CopyUtils.copyObj(dto, NoticeVillageEntity.class);
        entity.setId(Snowflake.nextId());
        noticeVillageDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(NoticeVillageDto dto) {
        NoticeVillageEntity entity = CopyUtils.copyObj(dto, NoticeVillageEntity.class);
        noticeVillageDao.updateById(entity);
        CacheKit.invalidRedisCache(NOTICE_VILLAGE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(NoticeVillageDto dto) {
        NoticeVillageEntity entity = CopyUtils.copyObj(dto, NoticeVillageEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, noticeVillageDao);

        CacheKit.invalidRedisCache(NOTICE_VILLAGE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        noticeVillageDao.deleteById(id);

        CacheKit.invalidRedisCache(NOTICE_VILLAGE_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
